package com.lenovo.smarttraffic.util;

import android.content.Context;
import android.widget.Toast;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;
import com.lenovo.smarttraffic.InitApp;

import org.json.JSONException;
import org.json.JSONObject;

public class HttpUtils {

    public static String TAG = HttpUtils.class.getSimpleName();

    private static RequestQueue requestQueue = Volley.newRequestQueue(InitApp.getContext());
    private JSONObject jsonObject = new JSONObject();

    //JSON对象中添加参数
    public HttpUtils add(String key, Object value) {
        try {
            jsonObject.put(key, value);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return this;
    }

    public HttpUtils setParam(String key, Object value) {
        try {
            jsonObject.put(key, value);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return this;
    }

    public JSONObject getParam() {
        return jsonObject;
    }

    //回调接口
    public interface ICallBack {
        void success(JSONObject response) throws JSONException;
    }

    public interface CallBack {
        void success(JSONObject response) throws JSONException;
    }

    @Deprecated
    public static void post(Context context, String url, HttpUtils data, CallBack callBack) {
        //请求的链接
        String strUrl = "http://10.0.2.2:8080/api/v2/" + url;
        //需要请求的对象
        JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.POST,
                strUrl,
                data.getParam(),    //post请求的json参数
                response -> {     //请求成功的回调
                    try {
                        callBack.success(response);   //调用回调
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }, volleyError -> { //请失败的回调
            Toast.makeText(InitApp.getContext(), volleyError.toString(), Toast.LENGTH_LONG).show();
        });
        //进行请求
        requestQueue.add(jsonObjectRequest);
    }

    public static void post(String url, HttpUtils data, ICallBack callBack) {
        //请求的链接
        String strUrl = "http://10.0.2.2:8080/api/v2/" + url;
        //String strUrl = "http://192.168.123.1:8080/api/v2/" + url;
        //String strUrl = "http://10.0.2.2:8088/transportservice/action/" + url;
        //需要请求的对象
        JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.POST,
                strUrl,
                data.getParam(),    //post请求的json参数
                response -> {     //请求成功的回调
                    try {
                        callBack.success(response);   //调用回调
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }, volleyError -> { //请失败的回调
            Toast.makeText(InitApp.getContext(), volleyError.toString(), Toast.LENGTH_LONG).show();
        });
        //进行请求
        requestQueue.add(jsonObjectRequest);
    }

}
